package com.tutorial;

import java.util.List;

import com.db.DataBaseHelper;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class Item extends Activity implements LocationListener {
	public static int position;
	EditText location;
	GeoPoint p = null;
	MapController mc = null;
	LocationManager lm = null;
	DataBaseHelper db = new DataBaseHelper(this);

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.share);
		
		List<String> a = db.getOne("place", Item.position+1);
		// Lay gia tri cho vao EditText
		EditText name = (EditText) findViewById(R.id.place_id);
		name.setText(a.get(0));
		EditText address = (EditText) findViewById(R.id.address_id);
		address.setText(a.get(2));
		location = (EditText) findViewById(R.id.location_id);
		
		if (a.get(3) != null) {
			Log.v("A Location in Item.class", "hj");
			location.setText(a.get(3));
			String s = a.get(3);
			String s1[] = s.split(" ");
			Map.latitude = Float.parseFloat(s1[0]);
			Map.longitude = Float.parseFloat(s1[1]);
		} else {
			Log.v("A Location","fdhjfhf");
			location.setText("0.0  0.0");
			Map.longitude = 0;
			Map.latitude = 0;
		}
		EditText des = (EditText) findViewById(R.id.des_id);
		des.setText(a.get(4));
		db.close();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.item, menu);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		 switch(item.getItemId()){
		 case R.id.location_item:
			  lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
				lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
			
			 return true;
		 /*case R.id.delete_item:
				if(db.deleteLocation(Item.position+1)>0);
				{
					Log.v("Xoa"," "+(Item.position+1));
					startActivity(new Intent(this,LocationList.class));
					return true;
				}
				*/
		 case R.id.show_map:
			 Intent map = new Intent(this, Map.class);
			 startActivity(map);
			 return true;
		 }
		 return false;
	}

	public void onProviderDisabled(String provider) {
		// required for interface, not used
		Toast.makeText(getApplicationContext(),
		"Network Disabled", Toast.LENGTH_SHORT).show();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.location.LocationListener#onLocationChanged(android.location.
	 * Location)
	 */
	@SuppressWarnings("null")
	@Override
	public void onLocationChanged(Location loc) {
		// TODO Auto-generated method stub

		int i;
		Map.latitude = (float)loc.getLatitude();
		Map.longitude = (float)loc.getLongitude();
		location.setText(Map.latitude + " " + Map.longitude);
		/*
		 * p = new GeoPoint((int) latitude * 1000000, (int) longitude *
		 * 1000000); mc.animateTo(p);
		 */
		lm.removeUpdates(this);
		String value = Map.latitude + " " + Map.longitude;
		int id = Item.position + 1;

		i = db.upDateLocation(value, id);
		Log.v("A Updata", "i = " + i);
		db.close();

	}

	public void onProviderEnabled(String provider) {
		// required for interface, not used
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// required for interface, not used
	}

}
